/*
//  binfile1.c
//  MIPS Dissambler
//
//  Created by Daniel Diaz on 1/30/12.
//  Copyright 2012 UCI. All rights reserved.
*/



/*this part of the program will handle an incoming binfile and seperate every instruction and write it to a structure */

/* still needs testing */




#include "binfile1.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>



/*hexfilereader*/
/*
 system("objcopy -I ihex Hex.hex -0 binary Hex.bin");
*/


int main ()
{
    char *s;
    int length;
    printf("Input File Name:/n");
    scanf("%s", &s);
    
    length = strlen(s);
    char *d;
    d=malloc(3*sizeof(char));
    strcat(d,s[length-3]);
    strcat(d,s[length-2]);
    strcat(d,s[length-1]);
    if(strncmp(d,"bin",3==0)
       {
           
        }
       else if(strncmp(d,"bin",3==0))
       {
           
       }
       else if(strncmp(d,"bin",3==0))
       {
           
       }
       
   
    
    return 0;
}
       
ILIST * NewInstructionList(void)
    {
       ILIST *l;
       l = malloc(sizeof(ILIST));
       if (! l)
       { perror("Out of memory! Aborting...");
           exit(10);
       } /* fi */
       l->Length = 0;
       l->First = NULL;
       l->Last = NULL;
       return l;
       /* end of NewInstructionList */
    }

/* delete a instruction list (and all entries) */
void DeleteInstructionList(ILIST *l)
{
   IENTRY *e, *n;
   assert(l);
   e = l->First;
   while(e)
   { n = e->Next;
       free(e->Instruction);
       free(e);
       e = n;
   }
   free(l);
   /* end of DeleteImageList */
}

       
/* insert an instruction into a list */
void AppendInstruction(ILIST *l, int y)
{
   IENTRY *e = NULL;
   assert(l);
   assert(Instruction);
   e = malloc(sizeof(IENTRY));
   if (! e)
   { perror("Out of memory! Aborting...");
       exit(10);
   } /* fi */;
   
    e->instruction =y;
   if (l->Last)
   { e->List = l;
       e->Next = NULL;
       e->Prev = l->Last;
       l->Last->Next = e;
       l->Last = e;
   }
   else
   { e->List = l;
       e->Next = NULL;
       e->Prev = NULL;
       l->First = e;
       l->Last = e;
   }
   l->Length++;
} 





ReadBinFile (char * fname) 
{
    
    /*test file*/
    /*
    char fname[8] = "bin.bin";
    */
    
    long lSize;
    char * buffer;
    /* size_t result; */
    
    FILE *fp;
    fp=fopen(fname, "rb");
    
        
    /*read file*/
    if (fp)
    {
        
        /*initialize new instruction list*/
        ILIST * Instruction_List = NewInstructionList();
        
        
        /* obtain file size: */
        /*
        lSize = ftell (pFile)/4;
        Instruction_List->NumberInstructions=lSize;
        */
        
    
        /* while not EOF*/
        while (fp != EOF)
        {
        /*for loop, every 32 bits for instructions*/

            int x;
            fscanf(fp, "%d", &x);
                        AppendInstruction(Instruction_List, x);
            
            
        }
    }
    
    
}